package com.ssyc.common.view.cleanedittext;

import android.content.Context;
import android.content.res.TypedArray;
import android.text.Editable;
import android.text.InputFilter;
import android.text.InputType;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.ssyc.common.R;


/**
 * Created by jinjin on 16/6/2.
 */
public class CommonEditText extends RelativeLayout {

    private EditText edittext;
    private ImageView imageview;

    private TextWatcher textWatcher = new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
        }

        @Override
        public void afterTextChanged(Editable s) {
            if (listener != null) {
                listener.after();
            }
        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            //在输入框没有输入时不显示清除图标，有输入后显示
            if (TextUtils.isEmpty(s)) {
                imageview.setVisibility(View.INVISIBLE);
            } else {
                imageview.setVisibility(View.VISIBLE);
            }
        }
    };

    public CommonEditText(Context context) {
        super(context);
    }

    public CommonEditText(Context context, AttributeSet attrs) {
        super(context, attrs);
        View view = LayoutInflater.from(context).inflate(R.layout.weight_common_edittext, this, true);

        edittext = (EditText) view.findViewById(R.id.edittext);
        imageview = (ImageView) view.findViewById(R.id.imageview);

        edittext.setLongClickable(false);
        edittext.setTextIsSelectable(false);

        edittext.addTextChangedListener(textWatcher);
        imageview.setVisibility(View.INVISIBLE);

        imageview.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                edittext.setText("");
            }
        });

        TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.CommonEditText);

        //rightImg
        int rightImageR = ta.getResourceId(R.styleable.CommonEditText_cetRightImg, -1);
        if (rightImageR == -1) {
            imageview.setVisibility(View.GONE);
        } else {
            imageview.setImageResource(rightImageR);
        }

        //hint
        int resourceHint = ta.getResourceId(R.styleable.CommonEditText_cetHint, 0);
        edittext.setHint(resourceHint > 0 ? getResources().getText(resourceHint) : ta.getString(R.styleable.CommonEditText_cetHint));

        //text
        int resourceText = ta.getResourceId(R.styleable.CommonEditText_cetText, 0);
        edittext.setText(resourceText > 0 ? getResources().getText(resourceText) : ta.getString(R.styleable.CommonEditText_cetText));

        //inputType
        int resourceInputType = ta.getInt(R.styleable.CommonEditText_cetInputType, -1);
        edittext.setSingleLine();
        if (resourceInputType == 1) {//phone
            edittext.setInputType(InputType.TYPE_CLASS_PHONE);
        } else if (resourceInputType == 2) {//password
//          edittext.setInputType(InputType.TYPE_TEXT_VARIATION_PASSWORD );
            edittext.setInputType(InputType.TYPE_TEXT_VARIATION_PASSWORD |InputType.TYPE_CLASS_TEXT);
        } else if (resourceInputType == 3) {//text
            edittext.setInputType(InputType.TYPE_CLASS_TEXT);
        } else if (resourceInputType == 4) {//number
            edittext.setInputType(InputType.TYPE_CLASS_NUMBER);
        }

        // maxLength
        int maxLength = ta.getInt(R.styleable.CommonEditText_cetMaxLength, -1);
        if (maxLength != -1) {
            edittext.setFilters(new InputFilter.LengthFilter[]{new InputFilter.LengthFilter(maxLength)});
        }

        // textSize
        float textSize = ta.getDimension(R.styleable.CommonEditText_cetTextSize, -1);
        if (textSize != -1) {
            edittext.setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize);
        }

        ta.recycle();
    }


    public Editable getText() {
        return edittext.getText();
    }

    public EditText getEdittext() {
        return edittext;
    }

    public void setText(int resid) {
        edittext.setText(resid);
    }

    public void setText(CharSequence text) {
        edittext.setText(text);
    }

    public void setTextcolor(int id) {
        edittext.setTextColor(id);
    }

//    public void setHintcolor(int id)
//    {
//        edittext.setHintTextColor(id);
//    }

    public void setEditSelection(int index) {
        edittext.setSelection(index);
    }

    public void setImeOptions(int imeOptions) {
        edittext.setImeOptions(imeOptions);
    }

    public void addTextChangedListener(TextWatcher textWatcher) {
        edittext.addTextChangedListener(textWatcher);
    }

    public void setNextFocusForward(int nextFocusForwardId) {
        edittext.setNextFocusForwardId(nextFocusForwardId);
    }

    @Override
    public void setEnabled(boolean enabled) {
        super.setEnabled(enabled);
        edittext.setEnabled(enabled);
    }


    public onAfterInputListener listener;

    public interface onAfterInputListener {
        void after();
    }

    public void setListener(onAfterInputListener listener) {
        this.listener = listener;
    }
}
